class Solution:
    def countSubTrees(self, n: int, edges, labels: str):
        linknodes=[[]for i in range(n)]
        visited=[0 for i in range(10)]
        results=[]
        for e in edges:
            if visited[e[1]]==1 and visited[e[0]]==0:
                linknodes[e[1]].append(e[0])
            else:
                linknodes[e[0]].append(e[1])
            visited[e[1]]=1
            visited[e[0]]=1
        for i in range(n):
            cand=[i]+linknodes[i]
            count=1
            while len(cand)!=0:
                for j in linknodes[cand[0]]:
                    if labels[j]==labels[i]:
                        count+=1
                    cand+=linknodes[j]
                cand.pop(0)
            results.append(count)
        return results